

#import "EventListener.h"
#import "SocketEngine.h"
#import "ClientProtocol.h"


@implementation EventListener

- (id)init
{
	[super init];
	m_cmdQuantity = 0;
	m_cmdMap = nil;
	return self;
}

-(void)dealloc
{
	if(m_cmdQuantity>0) free(m_cmdMap);
	[super dealloc];
}

- (BOOL)CreateMap: (int) quantity
{
	if(m_cmdQuantity>0) free(m_cmdMap);

	m_cmdMap = malloc(sizeof(CMDFUN)*quantity);
	if(m_cmdMap==NULL) return false;
	m_cmdQuantity = quantity;
	memset(m_cmdMap,0,sizeof(CMDFUN)*quantity);
	return YES;
}

- (BOOL)AddCmd: (int) cmdId fun: (CMDFUN) cmdFun 
{
	if(cmdId<0 || cmdId>=m_cmdQuantity) return NO;
	m_cmdMap[cmdId] = cmdFun;
	return YES;
}

- (void)OnConncet: (SocketEngine *) socket
{
	NSLog(@"Onconnect");
}

- (void)OnDisconncet: (SocketEngine *) socket
{
	NSLog(@"OnDisconnect");
}

- (void)OnRecv: (SocketEngine *) socket cmdId: (int) cmdId msgLen: (size_t) msgLen
{
	NSLog(@"OnRecv:%d",cmdId);
	if(cmdId>=0 && cmdId<m_cmdQuantity && m_cmdMap[cmdId])
	{
		(*m_cmdMap[cmdId])(socket);
	}
}

@end
